﻿using System;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;
using Maticsoft.DBUtility;//Please add references
namespace hcrainMvc.DAL
{
	/// <summary>
	/// 数据访问类:hc_de
	/// </summary>
	public partial class hc_de
	{
		public hc_de()
		{}
		#region  BasicMethod

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(long fid)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from hc_de");
			strSql.Append(" where fid=@fid");
			MySqlParameter[] parameters = {
					new MySqlParameter("@fid", MySqlDbType.Int64)
			};
			parameters[0].Value = fid;

			return DbHelperMySQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(hcrainMvc.Model.hc_de model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into hc_de(");
			strSql.Append("mid,dename,datatypeid,adwidth,adheight,sqlfieldas,sqltableinfo,wfmid,listmid,aumid,son1aumid,son1listmid,son1datatypeid,son1name)");
			strSql.Append(" values (");
			strSql.Append("@mid,@dename,@datatypeid,@adwidth,@adheight,@sqlfieldas,@sqltableinfo,@wfmid,@listmid,@aumid,@son1aumid,@son1listmid,@son1datatypeid,@son1name)");
			MySqlParameter[] parameters = {
					new MySqlParameter("@mid", MySqlDbType.Double),
					new MySqlParameter("@dename", MySqlDbType.VarChar,50),
					new MySqlParameter("@datatypeid", MySqlDbType.Double),
					new MySqlParameter("@adwidth", MySqlDbType.Double),
					new MySqlParameter("@adheight", MySqlDbType.Double),
					new MySqlParameter("@sqlfieldas", MySqlDbType.VarChar,255),
					new MySqlParameter("@sqltableinfo", MySqlDbType.VarChar,255),
					new MySqlParameter("@wfmid", MySqlDbType.VarChar,50),
					new MySqlParameter("@listmid", MySqlDbType.Double),
					new MySqlParameter("@aumid", MySqlDbType.Double),
					new MySqlParameter("@son1aumid", MySqlDbType.Double),
					new MySqlParameter("@son1listmid", MySqlDbType.Double),
					new MySqlParameter("@son1datatypeid", MySqlDbType.Double),
					new MySqlParameter("@son1name", MySqlDbType.VarChar,50)};
			parameters[0].Value = model.mid;
			parameters[1].Value = model.dename;
			parameters[2].Value = model.datatypeid;
			parameters[3].Value = model.adwidth;
			parameters[4].Value = model.adheight;
			parameters[5].Value = model.sqlfieldas;
			parameters[6].Value = model.sqltableinfo;
			parameters[7].Value = model.wfmid;
			parameters[8].Value = model.listmid;
			parameters[9].Value = model.aumid;
			parameters[10].Value = model.son1aumid;
			parameters[11].Value = model.son1listmid;
			parameters[12].Value = model.son1datatypeid;
			parameters[13].Value = model.son1name;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(hcrainMvc.Model.hc_de model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update hc_de set ");
			strSql.Append("mid=@mid,");
			strSql.Append("dename=@dename,");
			strSql.Append("datatypeid=@datatypeid,");
			strSql.Append("adwidth=@adwidth,");
			strSql.Append("adheight=@adheight,");
			strSql.Append("sqlfieldas=@sqlfieldas,");
			strSql.Append("sqltableinfo=@sqltableinfo,");
			strSql.Append("wfmid=@wfmid,");
			strSql.Append("listmid=@listmid,");
			strSql.Append("aumid=@aumid,");
			strSql.Append("son1aumid=@son1aumid,");
			strSql.Append("son1listmid=@son1listmid,");
			strSql.Append("son1datatypeid=@son1datatypeid,");
			strSql.Append("son1name=@son1name");
			strSql.Append(" where fid=@fid");
			MySqlParameter[] parameters = {
					new MySqlParameter("@mid", MySqlDbType.Double),
					new MySqlParameter("@dename", MySqlDbType.VarChar,50),
					new MySqlParameter("@datatypeid", MySqlDbType.Double),
					new MySqlParameter("@adwidth", MySqlDbType.Double),
					new MySqlParameter("@adheight", MySqlDbType.Double),
					new MySqlParameter("@sqlfieldas", MySqlDbType.VarChar,255),
					new MySqlParameter("@sqltableinfo", MySqlDbType.VarChar,255),
					new MySqlParameter("@wfmid", MySqlDbType.VarChar,50),
					new MySqlParameter("@listmid", MySqlDbType.Double),
					new MySqlParameter("@aumid", MySqlDbType.Double),
					new MySqlParameter("@son1aumid", MySqlDbType.Double),
					new MySqlParameter("@son1listmid", MySqlDbType.Double),
					new MySqlParameter("@son1datatypeid", MySqlDbType.Double),
					new MySqlParameter("@son1name", MySqlDbType.VarChar,50),
					new MySqlParameter("@fid", MySqlDbType.Int64,20)};
			parameters[0].Value = model.mid;
			parameters[1].Value = model.dename;
			parameters[2].Value = model.datatypeid;
			parameters[3].Value = model.adwidth;
			parameters[4].Value = model.adheight;
			parameters[5].Value = model.sqlfieldas;
			parameters[6].Value = model.sqltableinfo;
			parameters[7].Value = model.wfmid;
			parameters[8].Value = model.listmid;
			parameters[9].Value = model.aumid;
			parameters[10].Value = model.son1aumid;
			parameters[11].Value = model.son1listmid;
			parameters[12].Value = model.son1datatypeid;
			parameters[13].Value = model.son1name;
			parameters[14].Value = model.fid;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(long fid)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from hc_de ");
			strSql.Append(" where fid=@fid");
			MySqlParameter[] parameters = {
					new MySqlParameter("@fid", MySqlDbType.Int64)
			};
			parameters[0].Value = fid;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string fidlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from hc_de ");
			strSql.Append(" where fid in ("+fidlist + ")  ");
			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public hcrainMvc.Model.hc_de GetModel(long fid)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select fid,mid,dename,datatypeid,adwidth,adheight,sqlfieldas,sqltableinfo,wfmid,listmid,aumid,son1aumid,son1listmid,son1datatypeid,son1name from hc_de ");
			strSql.Append(" where fid=@fid");
			MySqlParameter[] parameters = {
					new MySqlParameter("@fid", MySqlDbType.Int64)
			};
			parameters[0].Value = fid;

			hcrainMvc.Model.hc_de model=new hcrainMvc.Model.hc_de();
			DataSet ds=DbHelperMySQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public hcrainMvc.Model.hc_de DataRowToModel(DataRow row)
		{
			hcrainMvc.Model.hc_de model=new hcrainMvc.Model.hc_de();
			if (row != null)
			{
				if(row["fid"]!=null && row["fid"].ToString()!="")
				{
					model.fid=long.Parse(row["fid"].ToString());
				}
					//model.mid=row["mid"].ToString();
				if(row["dename"]!=null)
				{
					model.dename=row["dename"].ToString();
				}
					//model.datatypeid=row["datatypeid"].ToString();
					//model.adwidth=row["adwidth"].ToString();
					//model.adheight=row["adheight"].ToString();
				if(row["sqlfieldas"]!=null)
				{
					model.sqlfieldas=row["sqlfieldas"].ToString();
				}
				if(row["sqltableinfo"]!=null)
				{
					model.sqltableinfo=row["sqltableinfo"].ToString();
				}
				if(row["wfmid"]!=null)
				{
					model.wfmid=row["wfmid"].ToString();
				}
					//model.listmid=row["listmid"].ToString();
					//model.aumid=row["aumid"].ToString();
					//model.son1aumid=row["son1aumid"].ToString();
					//model.son1listmid=row["son1listmid"].ToString();
					//model.son1datatypeid=row["son1datatypeid"].ToString();
				if(row["son1name"]!=null)
				{
					model.son1name=row["son1name"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select fid,mid,dename,datatypeid,adwidth,adheight,sqlfieldas,sqltableinfo,wfmid,listmid,aumid,son1aumid,son1listmid,son1datatypeid,son1name ");
			strSql.Append(" FROM hc_de ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperMySQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM hc_de ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.fid desc");
			}
			strSql.Append(")AS Row, T.*  from hc_de T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperMySQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			MySqlParameter[] parameters = {
					new MySqlParameter("@tblName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@fldName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@PageSize", MySqlDbType.Int32),
					new MySqlParameter("@PageIndex", MySqlDbType.Int32),
					new MySqlParameter("@IsReCount", MySqlDbType.Bit),
					new MySqlParameter("@OrderType", MySqlDbType.Bit),
					new MySqlParameter("@strWhere", MySqlDbType.VarChar,1000),
					};
			parameters[0].Value = "hc_de";
			parameters[1].Value = "fid";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperMySQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

